package com.qingke.oms.command.shop;

import java.util.List;

import com.qingke.oms.command.ShopCommand;
import com.qingke.oms.pojo.Shop;
import com.qingke.oms.pojo.ShoppingCart;
import com.qingke.oms.pojo.ShoppingCartItem;
import com.qingke.oms.pojo.User;
import com.qingke.oms.util.OMSConsole;

public class EmptyShoppingCartCommand extends ShopCommand {

	public EmptyShoppingCartCommand(User user, Shop shop) {
		super(user, shop);
	}

	@Override
	public void execute() {
		ShoppingCart shoppingCart=dao.selectShoppingCart(user.getId(), shop.getId());
		if(shoppingCart==null){
			OMSConsole.println("you have never add goods to shopping cart.");
			OMSConsole.println("so you don't have the shopping cart in the shop.");
			return ;
		}
		List<ShoppingCartItem> items=shoppingCart.getItems();
		if(items.size()==0){
			OMSConsole.println("your shopping cart is empty , doesn't need to empty.");
			return ;
		}
		
		String cmd;
		OMSConsole.println("are you sure empty shopping cart?");
		while(true){
			cmd=OMSConsole.promptNotEmpty("sure>").trim();
			if("yes".equalsIgnoreCase(cmd)){
				break;
			}
			else if("no".equalsIgnoreCase(cmd)){
				OMSConsole.println("cancel place empty success.");
				return ;
			}
			else if("help".equalsIgnoreCase(cmd)){
				OMSConsole.println("- yes    sure to empty shopping cart");
				OMSConsole.println("- no     cancel to empty shopping cart");
			}
			else{
				OMSConsole.println("err input");
			}
		}
		
		boolean isSuccess=dao.updateShoppingCartItemBeCleared(shoppingCart);
		if(!isSuccess){
			OMSConsole.println("empty failed");
			return ;
		}
		OMSConsole.println("empty success.");
	}

}
